import 'package:dsp_bt_audio/modules/dialog_bluetooth.dart';
import 'package:dsp_bt_audio/modules/dialog_infomation.dart';
import 'package:dsp_bt_audio/modules/dialog_setup.dart';
import 'package:dsp_bt_audio/modules/model.dart';
import 'package:dsp_bt_audio/modules/slider_item.dart';
import 'package:dsp_bt_audio/tool/assets.dart';
import 'package:flutter_kit_dsp/flutter_kit_dsp.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';

import '../common/chat_page.dart';
import 'my_home_controller.dart';

class MyHome extends HomePageDelegate<MyHomeController> {
  MyHome({super.key});
  @override
  void onInit(BuildContext context) {
    super.onInit(context);
    controller.dialog_app = DialogInfomation();
    controller.dialog_bluetooth = DialogBluetooth();
    controller.dialog_setup = DialogSetup();
  }

  Widget columnItem({required Widget child}) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(20),
        boxShadow: [BoxShadow(color: Colors.black, blurRadius: 2, spreadRadius: 0)],
      ),
      child: child,
    );
  }

  Widget sliderItem(List<Model> list) {
    return Column(
      children: list.map(($0) {
        return SliderItem(
          model: $0,
          onChange: (value, isSend) => controller.onSliderChange($0, value, issend: isSend),
          onStepChange: (isLeft) => controller.onStepChange($0, isleft: isLeft),
        );
      }).toList(),
    );
  }

  @override
  PreferredSizeWidget? get appBar {
    return AppBar(
      leadingWidth: 100,
      backgroundColor: Colors.white,
      leading: Builder(
        builder: (context) {
          return LongPressGestureDetector(
            duration: Duration(milliseconds: 3000),
            child: Image.asset(assets.logo),
            onLongPress: () => LogRecorder.toggle(context),
          );
        },
      ),
      actions: controller.items.asMap().entries.map(($0) {
        return Builder(
          builder: (context) {
            return Padding(
              padding: EdgeInsetsGeometry.only(left: 5, right: 5),
              child: Material(
                color: Colors.transparent,
                child: InkWell(
                  borderRadius: BorderRadius.circular(100),
                  onTap: () => controller.selectNavibarIndex($0.key, context),
                  child: Obx(() => Image.asset(controller.items[$0.key], width: 30, height: 30)),
                ),
              ),
            );
          },
        );
      }).toList(),
    );
  }

  @override
  Widget? buildBody(BuildContext context, Widget child) {
    return SafeArea(left: false, bottom: false, right: false, top: false, child: child);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.all(10),
      child: Column(
        spacing: 10,
        children: [
          Flexible(flex: 3, child: columnItem(child: Obx(() => sliderItem(controller.list1)))),
          Flexible(
            flex: 4,
            child: columnItem(
              child: ChartPage(
                onMove: (freq, gain, index, isMove) => controller.selectSignChange(index, freq, gain, isMove),
              ),
            ),
          ),
          Flexible(
            flex: 4,
            child: columnItem(
              child: Column(
                children: [
                  Container(
                    height: 60,
                    padding: EdgeInsets.only(left: 10, right: 10, top: 10),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      spacing: 5,
                      children: [
                        Container(width: 90, child: Obx(() => Text(controller.reset_title.value))),
                        myIconButton(
                          width: 30,
                          height: 30,
                          iconName: assets.refresh,
                          onTap: () => controller.resetSign(),
                        ),
                      ],
                    ),
                  ),
                  Expanded(child: Obx(() => sliderItem(controller.list2))),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
